<script>
    $(function(){
        var form = new Vue({
            el:'#form',
            data:{
                head_img : window.head_img,
                head_chang_to : window.detail.head_chang_to,
                deliver_address : window.detail.deliver_address,
                member_num : window.detail.member_num,
                car_num : window.detail.car_num,
            },
            watch: {
                deliver_address: function (value) {
                    if (!validator.element($('[name=deliver_address]'))){
                        return;
                    }
                    this.update('deliver_address');
                },
                member_num: function (value) {
                    if (!validator.element($('[name=member_num]'))){
                        return;
                    }
                    this.update('member_num');
                },
                car_num: function (value) {
                    if (!validator.element($('[name=car_num]'))){
                        return;
                    }
                    this.update('car_num');
                },
            },
            methods:{
                /**
                 * 更改头像
                 * @param event
                 */
                headImage:function(event){
                    var self = this;
                    var that = event.target;
                    var localId;

                    var promise = new Promise(function (resolve, reject) {
                        wx.chooseImage({
                            count: 1, // 默认9
                            success: function (res) {
                                localId = res.localIds[0];
                                wx.uploadImage({
                                    localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得
                                    isShowProgressTips: 1,// 默认为1，显示进度提示
                                    success: function (res) {
                                        var serverId = res.serverId; // 返回图片的服务器端ID
                                        resolve(serverId);
                                    }
                                });
                            }
                        });
                    }).then(function (value) {
                        axios.post('/ajax/master/changheadimg', {headimg:value})
                            .then(function (response) {
                                if (response.data.code == 200) {
                                    alert('修改成功，请等待审核');
                                    self.head_chang_to = localId;
                                } else {
                                    alert(response.data.message || '修改失败，请重新操作');
                                }
                            },function (response) {
                                alert(response.data.message || '修改失败，请重新操作');
                            }).catch(function (err) {
                            alert('修改失败，请重新操作');
                        });
                    }).catch(function(err){
                        alert('修改失败，请重新操作');
                    });
                },
                /**
                 * 当前页面部分数据即时更新
                 */
                update:_.debounce(function(dom_name){
//                    if (!validator.form()){
//                        return;
//                    }

                    var dom_value = $('[name='+dom_name+']').val();
                    if ((dom_value == window.detail[dom_name]) || (!dom_value && !window.detail[dom_name])){
                        return;
                    }

                    var post_field = {};
                    post_field[dom_name] = dom_value;
                    axios.post('/ajax/master/updatedetail',post_field
                    ).then(function (response) {
                        if (response.data.code == 200) {
                            window.detail[dom_name] = dom_value;
                        } else {
                            //
                        }
                    },function (response) {
                        //
                    }).catch(function (err) {
                        //
                    });
                },1000),
            }
        });

        var validator = $("#form").validate({
            rules: {
                deliver_address:{
                    includeChinese:true,
                },
                member_num:{
                    digits:true
                },
                car_num:{
                    digits:true
                },
            },
            messages: {
            },
            errorPlacement: function (error, element) {
                var error_element = element.parents('li:eq(0)').next('li.error');
                error_element.find('p').html(error);
                error_element.show();
            },
            success: function(label) {
                label.html("&nbsp;").parents('li:eq(0)').hide();
            },
            showErrors: function (errorMap, errorList) {
                if (errorList.length > 0) {
                    console.log(errorList[0].message);
                }
                this.defaultShowErrors();
            }
        });
    })
</script>
<style>
    li.error {display:none;}
    .ziliao_a input.error { border: 1px solid red; }
    label.error {color:red;}
</style>